package com.april.product.service.impl;

import com.april.common.utils.PageUtils;
import com.april.common.utils.Query;
import com.april.product.entity.AttrGroupEntity;
import com.april.product.mapper.AttrGroupMapper;
import com.april.product.service.AttrGroupService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.Objects;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupMapper, AttrGroupEntity> implements AttrGroupService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params, Long cateLogId) {
        IPage<AttrGroupEntity> page;
        if (cateLogId != null && cateLogId == 0) {
            page = this.page(
                    new Query<AttrGroupEntity>().getPage(params),
                    new QueryWrapper<AttrGroupEntity>()
            );
        } else {
            String key = (String) params.get("key");
            boolean isKey = StringUtils.isNotEmpty(key);
            LambdaQueryWrapper<AttrGroupEntity> wrapper = Wrappers.<AttrGroupEntity>lambdaQuery()
                    .eq(Objects.nonNull(cateLogId) && cateLogId > 0, AttrGroupEntity::getCatelogId, cateLogId)
                    .eq(isKey,AttrGroupEntity::getAttrGroupId, key).or().like(isKey,AttrGroupEntity::getAttrGroupName, key);
            // if (StringUtils.isNotEmpty(key)) {
            //     wrapper.and((obj) ->
            //             obj.eq(AttrGroupEntity::getAttrGroupId, key).or().like(AttrGroupEntity::getAttrGroupName, key));
            //     // wrapper.or(i -> i.eq(AttrGroupEntity::getAttrGroupId, key).like(AttrGroupEntity::getAttrGroupName, key));
            // }
            page = this.page(new Query<AttrGroupEntity>().getPage(params), wrapper);
        }
        return new PageUtils(page);
    }

}